<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .main {
            width: 400px;
            margin: 0 auto;
            text-align: center;
        }
        .list {
            border: 1px solid #999;
            min-height: 400px;
            margin-bottom: 20px;
            text-align: left;
        }
        .text {
        	font-size: 28px;
        	color: red;
        }
        .date {
        	float: right;
        }
    </style>
</head>

<body>
    <div class="main">
        <ol class="list" id="list">
        		
        </ol>
        <input type="text" id="inp" onkeyup="enter();">
        <input type="button" value="提交" onclick="submit();">
    </div>
</body>
</html>
<script src="js/util.js"></script>
<script>
    var inp = document.getElementById('inp');
    var list = document.getElementById('list');
    function submit() {
    	// 获取输入内容
		var text = inp.value;
		if (!text) {
			alert('请输入留言');
			return false;
		}
		
        // 获取插入时间并格式化
		var date = new Date();
		var dateStr = formatDate2(date);
		
		// 创建li节点
		var li = document.createElement('li');
		// 把输入内容和时间放入li里面
		li.innerHTML = `
		<span class="text">${text}</span>
		<span class="date">${dateStr}</span>`;
        // 获取ol的非空节点
        var lis = list.children;
        // 把li插入到ol的最前面, lis[0], 默认调用appendChild
        list.insertBefore(li, lis[0]);
        inp.value = '';
    }
    
    function enter() {
    	if (event.keyCode === 13) {
    		submit();
    	}
    }
</script>